package com.meichis.yndms.adapter.RecycleViewItemType;

import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.meichis.mcsappframework.adapter.recyclerview.base.ItemViewDelegate;
import com.meichis.mcsappframework.adapter.recyclerview.base.ViewHolder;
import com.meichis.yndms.R;
import com.meichis.yndms.model.DicDataItem;
import com.meichis.yndms.model.InspectTemplate_Item;

import java.util.ArrayList;

/**
 * Created by pei on 2016/8/24.
 * 默认item
 */
public class FillMode3_EditText implements ItemViewDelegate<String> {

    private ArrayList<InspectTemplate_Item> items;
    private ArrayList<DicDataItem> dicDataItems;

    public FillMode3_EditText(ArrayList<InspectTemplate_Item> items, ArrayList<DicDataItem> dicDataItems) {
        this.items = items;
        this.dicDataItems = dicDataItems;
    }

    @Override
    public int getItemViewLayoutId() {
        return R.layout.fill_mode_3_edit;
    }

    @Override
    public boolean isForViewType(String s, int position) {
        return position != 0 && position > items.size() && position % (items.size() + 1) != 0 && items.get(getTempPosition(position)).getItem().getFillMode() != 6 && items.get(getTempPosition(position)).getItem().getFillMode() != 7;
    }

    @Override
    public void convert(final ViewHolder holder, final String s, int position) {
        final EditText et_answer = holder.getView(R.id.et_answer);
        et_answer.setInputType(InputType.TYPE_CLASS_TEXT);
        //填写内容
        try {
            String answer = dicDataItems.get(getDicPosition(position)).getResult().getItems().get(getTempPosition(position)).getFillValue();
            if (answer == null) {
                answer = items.get(getTempPosition(position)).getFillName();
            }
            dicDataItems.get(getDicPosition(position)).getResult().getItems().get(getTempPosition(position)).setFillName(answer);
            dicDataItems.get(getDicPosition(position)).getResult().getItems().get(getTempPosition(position)).setFillValue(answer);
            et_answer.setText(answer);
        } catch (Exception e) {

        }
        int inputType = InputType.TYPE_CLASS_TEXT;
        switch (items.get(getTempPosition(position)).getItem().getFillMode()) {
            case 1:
                inputType = InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL;
                break;
            case 8:
                inputType = InputType.TYPE_CLASS_NUMBER;
                break;
            default:
                break;
        }
        et_answer.setInputType(inputType);
        if (!holder.haveTextChangedListener(et_answer)) {
            holder.setTextChangedListener((TextView) holder.getView(R.id.et_answer), new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable editable) {
                    try {
                        dicDataItems.get(getDicPosition(holder.getAdapterPosition())).getResult().getItems().get(getTempPosition(holder.getAdapterPosition())).setFillName(editable.toString());
                        dicDataItems.get(getDicPosition(holder.getAdapterPosition())).getResult().getItems().get(getTempPosition(holder.getAdapterPosition())).setFillValue(editable.toString());
                    } catch (Exception e) {

                    }
                }
            });
        }
    }

    //竖坐标
    private int getDicPosition(int position) {
        return position / (items.size() + 1) - 1;
    }

    //横坐标
    private int getTempPosition(int position) {
        return position % (items.size() + 1) - 1;
    }
}
